//
//  ModifyPassWordViewController.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/4.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "ModifyPassWordViewController.h"
#import "UILabel+BasicAttributes.h"
#import "UIImage+Rotate.h"
#import "NSString+File.h"

@interface ModifyPassWordViewController ()
@property (nonatomic,strong)UITextField *oldPassWordTextFile;
@property (nonatomic,strong)UITextField *passWordTextFile;
@property (nonatomic,strong)UITextField *sureTextFile;
@end

@implementation ModifyPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationTitleWithTitle:@"修改密码"];
    [self loadUI];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipemethod:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
}
- (void)swipemethod:(UISwipeGestureRecognizer *)swipe
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)loadUI
{
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemBtnClick:)];
    [leftItem setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemBtnClick:)];
    [rightItem setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UIView *viewOne = [UIView new];
    [self.view addSubview:viewOne];
    [viewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(NavationBarHeight);
        make.height.mas_equalTo(40);
    }];
    UIView *lineOne = [UIView new];
    lineOne.backgroundColor = [UIColor colorWithRed:0.88f green:0.88f blue:0.88f alpha:1.00f];
    [viewOne addSubview:lineOne];
    [lineOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(viewOne);
        make.height.mas_equalTo(0.5f);
    }];
    
    
    UIView *viewFour = [UIView new];
    [self.view addSubview:viewFour];
    [viewFour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(viewOne.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    UIView *lineFour = [UIView new];
    lineFour.backgroundColor = [UIColor colorWithRed:0.88f green:0.88f blue:0.88f alpha:1.00f];
    [viewFour addSubview:lineFour];
    [lineFour mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(viewFour);
        make.height.mas_equalTo(0.5f);
    }];
    
    UIView *viewTwo = [UIView new];
    [self.view addSubview:viewTwo];
    [viewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(viewFour.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    UIView *lineTwo = [UIView new];
    lineTwo.backgroundColor = [UIColor colorWithRed:0.88f green:0.88f blue:0.88f alpha:1.00f];
    [viewTwo addSubview:lineTwo];
    [lineTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(viewTwo);
        make.height.mas_equalTo(0.5f);
    }];
    
    UIView *viewThree = [UIView new];
    [self.view addSubview:viewThree];
    [viewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(viewTwo.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    UIView *lineThree = [UIView new];
    lineThree.backgroundColor = [UIColor colorWithRed:0.88f green:0.88f blue:0.88f alpha:1.00f];
    [viewThree addSubview:lineThree];
    [lineThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(viewThree);
        make.height.mas_equalTo(0.5f);
    }];
    
    
   
    

    
    UILabel *titleNameLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f] andTextAlignment:left];
    titleNameLabel.text = @"用户名";
    [viewOne addSubview:titleNameLabel];
    [titleNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOne.mas_left).offset(kWidthOfPt(60));
        make.centerY.mas_equalTo(viewOne);
    }];
    
    UILabel *nameLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f] andTextAlignment:left];
    nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    [viewOne addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewOne.mas_left).offset(kWidthOfPt(60)+kScreenWidth*0.25);
        make.centerY.mas_equalTo(viewOne);
    }];
    
    UILabel *titlePassWordLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f] andTextAlignment:left];
    titlePassWordLabel.text = @"新密码";
    [viewTwo addSubview:titlePassWordLabel];
    [titlePassWordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewTwo.mas_left).offset(kWidthOfPt(60));
        make.centerY.mas_equalTo(viewTwo);
    }];
    
    _oldPassWordTextFile = [UITextField new];
    _oldPassWordTextFile.placeholder = @"请输入旧密码";
    _oldPassWordTextFile.secureTextEntry = YES;
    [_oldPassWordTextFile becomeFirstResponder];
    [viewFour addSubview:_oldPassWordTextFile];
    [_oldPassWordTextFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewFour.mas_left).offset(kWidthOfPt(60)+kScreenWidth*0.25);
        make.centerY.mas_equalTo(viewFour);
        make.right.mas_equalTo(viewFour);
    }];
    
    UILabel *oldTitleAboutLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f] andTextAlignment:left];
    oldTitleAboutLabel.text = @"旧密码";
    [viewFour addSubview:oldTitleAboutLabel];
    [oldTitleAboutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewFour.mas_left).offset(kWidthOfPt(60));
        make.centerY.mas_equalTo(viewFour);
    }];
    
    
    
    _passWordTextFile = [UITextField new];
    _passWordTextFile.secureTextEntry = YES;
    _passWordTextFile.placeholder = @"请输入新密码";
    [viewTwo addSubview:_passWordTextFile];
    [_passWordTextFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewTwo.mas_left).offset(kWidthOfPt(60)+kScreenWidth*0.25);
        make.centerY.mas_equalTo(viewTwo);
        make.right.mas_equalTo(viewTwo);
    }];
    
    UILabel *titleAboutLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.20f green:0.20f blue:0.20f alpha:1.00f] andTextAlignment:left];
    titleAboutLabel.text = @"确认密码";
    [viewThree addSubview:titleAboutLabel];
    [titleAboutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewThree.mas_left).offset(kWidthOfPt(60));
        make.centerY.mas_equalTo(viewThree);
    }];
    
    _sureTextFile = [UITextField new];
    _sureTextFile.placeholder = @"请再次输入新密码";
    _sureTextFile.secureTextEntry = YES;
    [viewThree addSubview:_sureTextFile];
    [_sureTextFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewTwo.mas_left).offset(kWidthOfPt(60)+kScreenWidth*0.25);
        make.centerY.mas_equalTo(viewThree);
        make.right.mas_equalTo(viewThree);
    }];
    
}
- (void)leftItemBtnClick:(UIBarButtonItem *)item
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightItemBtnClick:(UIBarButtonItem *)item
{
    [self.sureTextFile resignFirstResponder];
    [self.passWordTextFile resignFirstResponder];
    
    NSString *oldPda = [[NSUserDefaults standardUserDefaults] valueForKey:@"PassWord"];
    NSString *newPad = _passWordTextFile.text;
    NSString *surePad= _sureTextFile.text;
    
    if (![newPad isEqualToString:surePad]) {
        [YJProgressHUD showProgress:@"两次密码输入不一致" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    
    if (self.passWordTextFile.text.length == 0) {
        [YJProgressHUD showMessage:@"请输入密码" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    if (self.passWordTextFile.text.length<6) {
        [YJProgressHUD showMessage:@"密码长度小于6位" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    
    if (self.oldPassWordTextFile.text.length==0) {
        [YJProgressHUD showMessage:@"请输入旧密码" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    
    if (![self.oldPassWordTextFile.text isEqualToString:oldPda]) {
        [YJProgressHUD showMessage:@"旧密码错误" inView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
        });
        return;
    }
    
   
    
    
    NSDictionary *param = @{
                            @"oldPassword":oldPda,
                            @"password":newPad
                            };
    [YJProgressHUD hide];
    [YJProgressHUD showProgress:@"修改中..." inView:self.view];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_ModifyPW method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        [YJProgressHUD hide];
        if ( [responseObject[@"state"] integerValue] == 0) {
            [YJProgressHUD showProgress:responseObject[@"msg"] inView:self.view];
            [[NSUserDefaults standardUserDefaults] setObject:newPad forKey:@"PassWord"];
        }else{
            [YJProgressHUD showMessage:@"修改失败" inView:self.view];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [YJProgressHUD hide];
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSError * _Nonnull error) {
    }];

}



@end
